package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.WorkSpaceMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private WorkSpaceMapper workSpaceMapper;
    @Autowired
    private ReportMapper reportMapper;
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        //获取营业额
        Double turnover = workSpaceMapper.getTurnover(begin,end);

        //获取有效订单数
        Map map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status", Orders.COMPLETED);
            //有效订单数
        Integer validOrderCount = workSpaceMapper.getOrderCountByMap(map);
        map.put("status",null);
            //总订单数
        Integer totalOrderCount = workSpaceMapper.getOrderCountByMap(map);

        Double orderCompletionRate = 0.0;
        Double unitPrice = 0.0;
        if (validOrderCount != 0 && totalOrderCount != 0) {
            //获取订单完成率
            orderCompletionRate = Double.valueOf(validOrderCount) / Double.valueOf(totalOrderCount);
            //获取平均客单价
            unitPrice = Double.valueOf(turnover) / Double.valueOf(validOrderCount);
        }
        //新增用户数
        Integer newUsers = reportMapper.countByMap(map);

        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .unitPrice(unitPrice)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover == null ? 0 : turnover)
                .validOrderCount(validOrderCount)
                .build();
    }

    @Override
    public OrderOverViewVO getOVerViewOrders(LocalDateTime begin, LocalDateTime end) {
        OrderOverViewVO orderOverViewVO = workSpaceMapper.getStatusOfNumber(begin,end);
        return orderOverViewVO;
    }

    @Override
    public DishOverViewVO getOverViewDishes() {

        return workSpaceMapper.getOverViewDishes();
    }

    @Override
    public SetmealOverViewVO getOverViewSetmeals() {
        return workSpaceMapper.getOVerViewSetmeals();
    }


}
